﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSBTC.Model;
using MSBTC.ReportViewer;

namespace MSBTC.WinForms
{
    public partial class SupplierForm : Form
    {
        MDIParent mdipar = new MDIParent();
        private string Datamanip;
        PMSContext dbcontext = new PMSContext();
        Supplier dbsup = new Supplier();
        FormHelpers help = new FormHelpers();
        private int rowselect;

        public SupplierForm()
        {
            InitializeComponent();
        }


        private void SupplierForm_Load(object sender, EventArgs e)
        {
            RefreshFields();
            RefreshDB();
        }

        #region functions
        private void RefreshDB()
        {
            var sup = (from s in dbcontext.Suppliers
                       select s).ToList();

            LVSupplierInfo.Items.Clear();
            if (sup != null)
            {
                
                foreach (var sqlCus in sup)
                {
                    ListViewItem lvitems = LVSupplierInfo.Items.Add(sqlCus.Supplierid_PK.ToString());
                    lvitems.SubItems.Add(sqlCus.Supplier_Name.ToString());
                    lvitems.SubItems.Add(sqlCus.Address.ToString());
                    lvitems.SubItems.Add(sqlCus.City.ToString());
                    lvitems.SubItems.Add(sqlCus.Description.ToString());
                    lvitems.SubItems.Add(sqlCus.Email_Address.ToString());
                    lvitems.SubItems.Add(sqlCus.Contacts.ToString());
                    
                }
            }
            this.Refresh();
        }

        private void ClearFields()
        {
            txtID.Clear();
            txtSName.Clear();
            txtAddr.Clear();
            txtCity.Clear();
            txtDesc.Clear();
            txtEadd.Clear();
            txtSNum.Clear();
            txtSearch.Clear();
            btnSDel.Enabled = false;
            btnSEdit.Enabled = false;
            btnSSave.Enabled = false;
            btnSNew.Enabled = true;
        }

        private void DisableFields()
        {
            txtSName.Enabled = false;
            txtAddr.Enabled = false;
            txtCity.Enabled = false;
            txtDesc.Enabled = false;
            txtEadd.Enabled = false;
            txtSNum.Enabled = false;

        }

        private void EnableFields()
        {
            txtSName.Enabled = true;
            txtAddr.Enabled = true;
            txtCity.Enabled = true;
            txtDesc.Enabled = true;
            txtEadd.Enabled = true;
            txtSNum.Enabled = true;

        }

        private void RefreshFields()
        {
            DisableFields();
            ClearFields();
            RefreshDB();

            btnSNew.Enabled = true;
            btnSDel.Enabled = false;
            btnSEdit.Enabled = false;
            btnClear.Enabled = true;
            btnSSave.Enabled = false;
        }
        #endregion

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearFields();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshFields();
            LVSupplierInfo.Enabled = true;
        }

        private void btnSNew_Click(object sender, EventArgs e)
        {
            Datamanip = "Add";
            EnableFields();
            ClearFields();

            btnSSave.Enabled = true;
            btnSNew.Enabled = false;
            LVSupplierInfo.Enabled = false;
        }

        private void btnSEdit_Click(object sender, EventArgs e)
        {
            Datamanip = "Edit";

            EnableFields();

            btnSSave.Enabled = true;
            btnSNew.Enabled = false;
            btnSDel.Enabled = false;
            btnSEdit.Enabled = false;
            
        }

        private void btnSSave_Click(object sender, EventArgs e)
        {
            if (txtSName.Text == "" || txtAddr.Text == string.Empty || txtCity.Text == string.Empty || txtSNum.Text == string.Empty || txtEadd.Text == string.Empty)
            {
                MessageBox.Show("Please fill up the empty fields", "Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
               //validation for special character inputs
            else if (help.SpecChar(txtSName.Text, txtSName) == false  || help.SpecChar(txtCity.Text, txtCity) == false || help.SpecChar(txtSNum.Text, txtSNum) == false)
            {
                
                MessageBox.Show("Special Characters are not allowed", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else
            {




                if (Datamanip == "Add")
                {
                    var add = (from c in dbcontext.Suppliers
                               where c.Supplier_Name == txtSName.Text
                               select c).ToList();

                    if (add.Count == 0)
                    {
                        dbsup.Supplier_Name = txtSName.Text;
                        dbsup.Address = txtAddr.Text;
                        dbsup.City = txtCity.Text;
                        dbsup.Description = txtDesc.Text;
                        dbsup.Contacts = Convert.ToInt32(txtSNum.Text);
                        dbsup.Email_Address = txtEadd.Text;

                        dbcontext.Entry(dbsup).State = EntityState.Added;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();

                    }
                    else
                    {
                        foreach (var c in add)
                        {
                            if (c.Supplier_Name == txtSName.Text && c.Description == txtDesc.Text)
                            {
                                DialogResult YN = MessageBox.Show("You've entered an existing client, do you want to replace it?", "Data Exist", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                                if (YN == DialogResult.Yes)
                                {
                                    var replace = dbcontext.Customers.Find(c.Supplierid_PK);
                                    replace.Customer_Name = txtSName.Text;
                                    replace.Address = txtAddr.Text;
                                    replace.City = txtCity.Text;
                                    replace.Contacts = Convert.ToInt32(txtSNum.Text);
                                    replace.Email_Address = txtEadd.Text;
                                    replace.Description = txtDesc.Text;

                                    dbcontext.Entry(replace).State = EntityState.Modified;
                                    dbcontext.SaveChanges();

                                    MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                    RefreshFields();
                                }
                            }
                            else
                            {
                                dbsup.Supplier_Name = txtSName.Text;
                                dbsup.Address = txtAddr.Text;
                                dbsup.City = txtCity.Text;
                                dbsup.Description = txtDesc.Text;
                                dbsup.Contacts = Convert.ToInt32(txtSNum.Text);
                                dbsup.Email_Address = txtEadd.Text;

                                dbcontext.Entry(dbsup).State = EntityState.Added;
                                dbcontext.SaveChanges();

                                MessageBox.Show("Successfully Saved.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                RefreshFields();
                                RefreshDB();
                            }
                        }
                    }
                    LVSupplierInfo.Enabled = true;
                }

                else if (Datamanip == "Edit")
                {
                    DialogResult YN = MessageBox.Show("Do you want to replace it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (YN == DialogResult.Yes)
                    {
                        var replace = dbcontext.Suppliers.Find(Convert.ToInt32(txtID.Text));
                        replace.Supplier_Name = txtSName.Text;
                        replace.Address = txtAddr.Text;
                        replace.City = txtCity.Text;
                        replace.Contacts = Convert.ToInt32(txtSNum.Text);
                        replace.Email_Address = txtEadd.Text;
                        replace.Description = txtDesc.Text;

                        dbcontext.Entry(replace).State = EntityState.Modified;
                        dbcontext.SaveChanges();

                        MessageBox.Show("Successfully Modified.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        RefreshFields();
                        RefreshDB();
                    }
                }
            }


        }
        
        private void LVSupplierInfo_SelectedIndexChanged(object sender, EventArgs e)
        {
            rowselect = LVSupplierInfo.FocusedItem.Index;


            txtID.Text = LVSupplierInfo.Items[rowselect].SubItems[0].Text;
            txtSName.Text = LVSupplierInfo.Items[rowselect].SubItems[1].Text;
            txtAddr.Text = LVSupplierInfo.Items[rowselect].SubItems[2].Text;
            txtCity.Text = LVSupplierInfo.Items[rowselect].SubItems[3].Text;
            txtDesc.Text = LVSupplierInfo.Items[rowselect].SubItems[4].Text;
            txtEadd.Text = LVSupplierInfo.Items[rowselect].SubItems[5].Text;
            txtSNum.Text = LVSupplierInfo.Items[rowselect].SubItems[6].Text;

            btnSDel.Enabled = true;
            btnSEdit.Enabled = true;
            btnSSave.Enabled = false;
            btnSNew.Enabled = true;

        }

        private void btnSDel_Click(object sender, EventArgs e)
        {
            DialogResult YN = MessageBox.Show("Do you want to Delete it?", "Modify", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (YN == DialogResult.Yes)
            {
                var del = dbcontext.Suppliers.Find(Convert.ToInt32(txtID.Text));
                if (del != null)
                {
                    dbcontext.Entry(del).State = EntityState.Deleted;
                    dbcontext.SaveChanges();
                }

            }
            RefreshFields();
            RefreshDB();
        }

        private void txtSearch_TextChanged_1(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                RefreshDB();
            }
            else
            {
                //retrieve the same item name search by user
                var purch = (from p in dbcontext.Suppliers
                             where p.Supplier_Name.Contains(txtSearch.Text)
                             select p).ToList();

                LVSupplierInfo.Items.Clear();
                if (purch.Count != 0)
                {

                    {

                        foreach (var sqlCus in purch)
                        {
                            ListViewItem lvitems = LVSupplierInfo.Items.Add(sqlCus.Supplierid_PK.ToString());
                            lvitems.SubItems.Add(sqlCus.Supplier_Name.ToString());
                            lvitems.SubItems.Add(sqlCus.Address.ToString());
                            lvitems.SubItems.Add(sqlCus.City.ToString());
                            lvitems.SubItems.Add(sqlCus.Description.ToString());
                            lvitems.SubItems.Add(sqlCus.Email_Address.ToString());
                            lvitems.SubItems.Add(sqlCus.Contacts.ToString());

                        }
                    }

                }


            }
        }

        private void btnSPrint_Click(object sender, EventArgs e)
        {
            SuppRptForm spForm = new SuppRptForm();

            spForm.Show();
        }


    }
}
